package city.spring.modules.reception.controller;

import city.spring.modules.reception.entity.ReservationEntity;
import city.spring.modules.reception.service.ReservationService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：预约登记
 *
 * @author 谢金婷
 * @date 2020-04-28 10:39:54
 */
@RestController
@RequestMapping("/reservation")
public class ReservationController extends ApiController {
    private final ReservationService reservationService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<ReservationEntity, ?>> entityOrderFields;

    public ReservationController(ReservationService reservationService) {
        this.reservationService = reservationService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(ReservationEntity::getId);
        entityOrderFields.add(ReservationEntity::getOldmanId);
        entityOrderFields.add(ReservationEntity::getSelfcare);
        entityOrderFields.add(ReservationEntity::getReservationCheckintime);
        entityOrderFields.add(ReservationEntity::getBedType);
        entityOrderFields.add(ReservationEntity::getIsPrivateroom);
        entityOrderFields.add(ReservationEntity::getBed);
        entityOrderFields.add(ReservationEntity::getFamilyaddress);
        entityOrderFields.add(ReservationEntity::getReservationName);
        entityOrderFields.add(ReservationEntity::getTelephone);
        entityOrderFields.add(ReservationEntity::getRelationshipOldman);
        entityOrderFields.add(ReservationEntity::getReservationtime);
        entityOrderFields.add(ReservationEntity::getStatus);
    }

    /**
     * 获取全部的 <strong>预约登记</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, ReservationEntity entity) {
        LambdaQueryChainWrapper<ReservationEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>预约登记</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, ReservationEntity entity) {
        LambdaQueryChainWrapper<ReservationEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<ReservationEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>预约登记</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(reservationService.getById(id));
    }

    /**
     * 添加一个 <strong>预约登记</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody ReservationEntity entity) {
        reservationService.saveReservation(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>预约登记</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody ReservationEntity entity) {
        entity.setId(id);
        reservationService.updateReservation(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>预约登记</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        reservationService.deleteReservation(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>预约登记</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        reservationService.deleteReservation(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<ReservationEntity> buildLambdaQuery(ReservationEntity entity) {
        LambdaQueryChainWrapper<ReservationEntity> lambdaQuery = reservationService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), ReservationEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), ReservationEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSelfcare()), ReservationEntity::getSelfcare, entity.getSelfcare());
        lambdaQuery.like(entity.getReservationCheckintime() != null, ReservationEntity::getReservationCheckintime, entity
                .getReservationCheckintime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getBedType()), ReservationEntity::getBedType, entity.getBedType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getIsPrivateroom()), ReservationEntity::getIsPrivateroom, entity.getIsPrivateroom());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getBed()), ReservationEntity::getBed, entity.getBed());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getFamilyaddress()), ReservationEntity::getFamilyaddress, entity.getFamilyaddress());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReservationName()), ReservationEntity::getReservationName, entity
                .getReservationName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTelephone()), ReservationEntity::getTelephone, entity.getTelephone());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRelationshipOldman()), ReservationEntity::getRelationshipOldman, entity
                .getRelationshipOldman());
        lambdaQuery.like(entity.getReservationtime() != null, ReservationEntity::getReservationtime, entity.getReservationtime());
        return lambdaQuery;
    }

}
